﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using SD.Database.Classes;
using System.Collections.Generic;
using DBAccess;

namespace SD.Database
{
    public partial class StoredProcedures
    {
        public static void UpdateIntel(int UserId)
        {
            DataSet ds = DAL.GetInstance().GetData("SELECT PlayerId, Attack, Defense, Speed, Culture, Production, Science, Growth, Biology, Economy, Energy, Math, Physics, Social, CurrentCulture FROM Players WHERE ((SELECT Biology FROM Players AS Players_1 WHERE (PlayerId = @param0)) >= Biology + (6 - (SELECT Academy FROM Players AS Players_2 WHERE (PlayerId = @param0)))) AND (PlayerId > 1)", new List<object>() { UserId });

            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count == 0) return;

            //process row data
            foreach (DataRow row in dt.Rows)
            {
                InsertScan(UserId, row);
            }

        }

        private static void InsertScan(int UserId, DataRow row)
        {
            DAL.GetInstance().DeleteData("Delete from Intel Where pId = @param0 and pId1 = @param1", new List<object>() { UserId, int.Parse(row[0].ToString()) });

            string scmd = "Insert Into Intel Values (@param0, @param1, @param2, @param3, @param4, @param5, @param6, @param7, @param8, @param9, @param10, @param11, @param12, @param13, @param14, @param15, @param16)";
            List<object> param = new List<object>(){ UserId, int.Parse(row[0].ToString()), DateTime.Now };
            for (int i = 1; i < 15; i++)
                param.Add(row[i]);
            DAL.GetInstance().InsertData(scmd, param);
        }

        public static bool SentMessage(int from, int to, string msg)
        {
            if (StaticMethods.ValidateMsg(msg) == true)
            {
                DAL.GetInstance().InsertData("Insert into Messages  Values(@param0, @param1, @param2, @param3)", new List<object>() { from, to, BBCodeHelper.Format(msg), DateTime.Now });

                InsertNews(to, DateTime.Now, "You have new messege from player: " + UserFunctions.PlayerName(from));
                return true;
            }
            return false;
        }

        public static void SaveInt(int pId, string property, int value)
        {
            DAL.GetInstance().UpdateData("Update Players SET " + property + " = @param0 Where PlayerID = @param1", new List<object>() { value, pId });

            InsertNews(8, DateTime.Now, "Update Players SET " + property + " = @c Where PlayerID = @p" + pId.ToString() + " " + value.ToString());
        }

        public static void UseArtifact(int pId, string aId)
        {
            DAL.GetInstance().UpdateData("Update Players SET ArtifactID = @param0 Where PlayerID = @param1", new List<object>() { aId, pId });
        }

        public static void BuyArtifact(int pId, string aId)
        {
            if (UserFunctions.GetPlayerFloat(pId, "Cash") < ArtifactsFunctions.Cost(aId))
            {
                return;
            }
            DAL.GetInstance().UpdateData("Update Players SET Cash = Cash - @param0 Where PlayerID = @param1", new List<object>() { ArtifactsFunctions.Cost(aId), pId });
            DAL.GetInstance().UpdateData("Insert into Artifacts Values (@param0, @param1)", new List<object>() { pId, aId });
        }

        public static void SellArtifact(int pId, string aId)
        {
            bool removeUsedArtifact = false;
            if (UserFunctions.UsedArtifact(pId).Equals(aId) == true)
                removeUsedArtifact = true;
            if (removeUsedArtifact == false)
                DAL.GetInstance().UpdateData("Update Players SET Cash = Cash + @param0 Where PlayerID = @param1", new List<object>() { ArtifactsFunctions.Cost(aId), pId });
            else
                DAL.GetInstance().UpdateData("Update Players SET Cash = Cash + @param0, ArtifactID='' Where PlayerID = @param1", new List<object>() { ArtifactsFunctions.Cost(aId), pId });

            DAL.GetInstance().DeleteData("Delete From Artifacts Where PlayerId = @param0 and ArtifactId = @param1", new List<object>() { pId, aId });
        }
        public static void RemoveAllNews(int pId)
        {
            DAL.GetInstance().DeleteData("Delete from News where PlayerId = @param0", new List<object>() { pId });
        }

        public static void ResignPlayer(int pId)
        {
            DAL dal = DAL.GetInstance();
            dal.UpdateData("Update planets Set OwnerId = 1 Where OwnerId = @param0", new List<object>() { pId });
            dal.UpdateData("Update planets Set FleetOwnerId = 1 Where FleetOwnerId = @param0", new List<object>() { pId });
            dal.DeleteData("Delete from News where PlayerId = @param0", new List<object>() { pId });
            dal.DeleteData("Delete from Fleets where OwnerId = @param0", new List<object>() { pId });
            dal.UpdateData("Update Battles Set AttackerID = 1 Where AttackerID = @param0", new List<object>() { pId });
            dal.UpdateData("Update Battles Set DefenderId = 1 Where DefenderId = @param0", new List<object>() { pId });
            dal.UpdateData("Update Battles Set WinnerId = 1 Where WinnerId = @param0", new List<object>() { pId });
            dal.DeleteData("Delete from Players Where PlayerId = @param0", new List<object>() { pId });
        }

        public static void SentMessageToAllPlayers(string msg)
        {
            List<int> tmp = new List<int>();
            DataSet ds = DAL.GetInstance().GetData("Select PlayerID From Players Where PlayerId > 1");
            if (ds.Tables.Count > 0)
                foreach (DataRow row in ds.Tables[0].Rows)
                    tmp.Add(int.Parse(row[0].ToString()));

            foreach (int i in tmp)
                InsertNews(i, DateTime.Now, msg);
        }


        public static void BuyBulding(int pId, Database.SpecialBuldings type)
        {
            int cost = 0;
            string command = "";
            switch (type)
            {
                case Database.SpecialBuldings.SpyAcademy: cost = 7500;
                    command = "Update players set Academy = 1, Cash = Cash -@param0 Where PlayerId = @param1";
                    break;
                case Database.SpecialBuldings.OrbitalCC: cost = 5000;
                    command = "Update players set OrbitalCenter = 1, Cash = Cash -@param0 Where PlayerId = @param1";
                    break;
                case Database.SpecialBuldings.AllianceCC: cost = 1000;
                    command = "Update players set AllianceCenter = 1, Cash = Cash -@param0 Where PlayerId = @param1";
                    break;
                case Database.SpecialBuldings.Market: cost = 7500;
                    command = "Update players set Market = 1, Cash = Cash -@param0 Where PlayerId = @param1";
                    break;
                case Database.SpecialBuldings.ScienceNetwork: cost = 15000;
                    command = "Update players set ScienceNetwork = 1, Cash = Cash -@param0 Where PlayerId = @param1";
                    break;
            }
            int cash = (int)UserFunctions.GetPlayerFloat(pId, "Cash");
            if (cost <= cash)
            {
                DAL.GetInstance().UpdateData(command, new List<object>() { cost, pId });
            }
        }

        public static void SetScience(int pId, int sciId)
        {
            DAL.GetInstance().UpdateData("Update Players Set CurrentScience = @param0 Where PlayerID =@param1", new List<object>() { sciId, pId });
        }

        public static void SetExperience(int pId, int exp)
        {
            int oldPts = UserFunctions.GetPlayerInt(pId, "ExpPoints");
            int oldLevel = Equations.PlayerLevel(oldPts);
            int newLevel = Equations.PlayerLevel(oldPts + exp);

            DAL.GetInstance().UpdateData("Update Players Set ExpPoints = ExpPoints + @param0 Where PlayerID =@param1", new List<object>() { exp, pId });
            if (newLevel - oldLevel > 0)
            {
                DAL.GetInstance().UpdateData("Update Players Set SupplyUnits = SupplyUnits + @param0 Where PlayerID =@param1", new List<object>() { newLevel - oldLevel, pId });
            }
        }
    };
}